//
//  CustomViewByActivity.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/3.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "CustomViewByActivity.h"
#import "CustomViewModelByAc.h"

@interface CustomViewModelByAc()

@end

@implementation CustomViewByActivity
{
    UIImageView *_imageView;
    UILabel *_titleLabel;
    UIImageView *_icon;
    UIImageView *_cityIcon;
    UILabel *_city;
    UILabel *_date;
    UILabel *_time;
    
}
//在initWithFrame:方法中添加子控件
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // 注意：该处不要给子控件设置frame与数据，可以在这里初始化子控件的属性
        _imageView = [[UIImageView alloc]init];
        [self addSubview:_imageView];
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_titleLabel];
        _icon = [[UIImageView alloc]init];
        _icon.layer.cornerRadius = 25/2;
        [self addSubview:_icon];
        _cityIcon = [[UIImageView alloc] init];
        [self addSubview:_cityIcon];
        _city = [[UILabel alloc]init];
        _city.textColor = [UIColor grayColor];
        _city.font = [UIFont systemFontOfSize:12];
        [self addSubview:_city];
        _date = [[UILabel alloc]init];
        _date.font = [UIFont systemFontOfSize:12];
        _date.textColor = [UIColor grayColor];
        [self addSubview:_date];
        _time = [[UILabel alloc]init];
        _time.textColor = [UIColor grayColor];
        _time.font = [UIFont systemFontOfSize:12];
        [self addSubview:_time];
    }
    return self;
}
//在`layoutSubviews`方法中设置子控件的`frame`（在该方法中一定要调用`[super layoutSubviews]`方法）
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //约束
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(8),
        make.left.mas_equalTo(8),
        make.width.mas_equalTo(85),
        make.height.mas_equalTo(55);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(8),
        make.left.mas_equalTo(_imageView.mas_right).offset(8),
        make.right.mas_equalTo(-8),
        make.height.mas_equalTo(20);
    }];
    
    [_icon mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(8),
        make.left.mas_equalTo(_imageView.mas_right).offset(8),
        make.height.mas_equalTo(25),
        make.width.mas_equalTo(25);
    }];
    
    [_time mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.mas_equalTo(self.mas_right).offset(-8),
        make.centerY.mas_equalTo(_icon.centerY),
        make.width.mas_equalTo(20),
        make.height.mas_equalTo(16);
    }];
    
    [_date mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.mas_equalTo(_time.mas_left).offset(-3),
        make.centerY.mas_equalTo(_icon.centerY),
        make.width.mas_equalTo(55),
        make.height.mas_equalTo(16);
    }];
    
    [_city mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.mas_equalTo(_date.mas_left).offset(-3),
        make.centerY.mas_equalTo(_icon.centerY),
        make.width.mas_equalTo(50),
        make.height.mas_equalTo(16);
    }];
    
    [_cityIcon mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.mas_equalTo(_city.mas_left).offset(-3),
        make.centerY.mas_equalTo(_icon.centerY),
        make.width.mas_equalTo(12),
        make.height.mas_equalTo(16);
    }];
    
}
//在该`setter`方法中取出模型属性，给对应的子控件赋值
- (void)setModel:(CustomViewModelByAc *)model
{
    _model = model;
    
    _imageView.image = [UIImage imageNamed:model.imageview];
    _titleLabel.text = model.title;
    _icon.image = [UIImage imageNamed:model.icon];
    _cityIcon.image = [UIImage imageNamed:model.cityIcon];
    _city.text = model.city;
    _date.text = model.date;
    _time.text = model.time;
}

@end
